package com.shijie.controller.app;

import java.util.List;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.NoLogin;
import com.shijie.domain.entity.Magazines;
import com.shijie.domain.entity.Signs;
import com.shijie.domain.enums.EnumInterface.IsUsed;
import com.shijie.service.MagazinesService;
import com.shijie.service.SignsService;

/*
 *  线下活动报名相关接口
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/signs")
public class AppSignsController {

    @Resource
	private SignsService signsService;
    @Resource
	private MagazinesService magazinesService;
	@Resource
	private AppUserService appUserService;
    
    /**
     * 活动报名人员列表
     * @param page
     * @param pageSize
     * @param id
     * @return
     */
	@GetMapping(value = "")
	public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam Integer id) {
		Magazines magazines = magazinesService.selectById(id);
		if (magazines == null) return ResultGenerator.error("报名活动不存在");
		PageHelper.startPage(page, pageSize);
		List<Signs> list = signsService.selectSignsByActivityId(magazines.getId());
		PageInfo<Signs> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}

	/**
	 * 我的报名
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value = "@user")
	public Result listForUser(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<Signs> list = signsService.selectSignsByUserId(appUserService.getCurrentUser().getId());
		PageInfo<Signs> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}


	/**
	 * 免费报名接口，如果是支付报名，支付回调后处理了
	 *
	 *
	 * @param activityId
	 * @return
	 */
	@PutMapping
    public Result add(@RequestParam Integer activityId) {
		try {
			Signs sign = new Signs();
			sign.setActivityId(activityId);
			signsService.sign(sign);
			return ResultGenerator.success();
		} catch (Exception e) {
			return ResultGenerator.error(e.getMessage());
		}
	}
}

